package com.itheima.app.manager;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.autoconfig.huanxin.HuanXinTemplate;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.UserLike;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.FriendService;
import com.itheima.service.mongo.UserLikeService;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.UserLikeCountVo;
import com.itheima.vo.UserLikeVo;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class UserLikeManager {

    @Reference
    private UserLikeService userLikeService;

    @Reference
    private FriendService friendService;

    @Autowired
    private HuanXinTemplate huanXinTemplate;

    @Reference
    private UserInfoService userInfoService;

    // 喜欢
    public void tanhuaLove(Long userLikeId) {
        // 1.获取登录用户id
        Long userId = UserHolder.get().getId();
        // 2.封装userLike对象
        UserLike userLike = new UserLike();
        userLike.setCreated(System.currentTimeMillis());
        userLike.setUserId(userId);
        userLike.setLikeUserId(userLikeId);
        // 3.调用rpc保存
        userLikeService.addUserlike(userLike);
        // 4.查询双方是否为相互喜欢
        Boolean mutualLike = userLikeService.isMutualLike(userId, userLikeId);
        // 5.如果是
        if (mutualLike) {
            friendService.addContacts(userId, userLikeId); // 添加mongo好友
            huanXinTemplate.addContacts(userId, userLikeId); // 添加环信好友
        }
    }

    // 不喜欢
    public void tanhuaUnlove(Long userLikeId) {
        // 1.获取登录用户id
        Long userId = UserHolder.get().getId();
        // 2.查询双方是否为相互喜欢
        Boolean mutualLike = userLikeService.isMutualLike(userId, userLikeId);
        // 3.如果是
        if (mutualLike) {
            friendService.removeContacts(userId, userLikeId); // 删除mongo好友
            huanXinTemplate.deleteContacts(userId, userLikeId); // 删除环信好友
        }
        // 4.调用rpc删除喜欢记录
        userLikeService.removeUserLike(userId, userLikeId);
    }

    // 统计数量
    public ResponseEntity userCounts() {
        // 1.获取登录userId
        Long userId = UserHolder.get().getId();
        // 2.调用rpc查询
        UserLikeCountVo userLikeCountVo = userLikeService.findCountVo(userId);

        // 3.返回
        return ResponseEntity.ok(userLikeCountVo);
    }

    // 统计详情
    public ResponseEntity userLikeVoDetail(Integer type, Integer pageNum, Integer pageSize) {
        // 1.获取登录用户id
        Long userId = UserHolder.get().getId();
        // 2.调用rpc查询
        PageBeanVo pageBeanVo = userLikeService.findMineUserLikeVoByPage(userId, type, pageNum, pageSize);

        // 3.完善vo
        List<UserLikeVo> voList = (List<UserLikeVo>) pageBeanVo.getItems();
        if (CollectionUtil.isNotEmpty(voList)) {
            for (UserLikeVo vo : voList) {
                // 根据id查询userInfo
                UserInfo userInfo = userInfoService.findByUserId(vo.getId());
                // 将用户信息copy到vo中
                BeanUtil.copyProperties(userInfo, vo);
            }
        }

        // 4.返回结果
        return ResponseEntity.ok(pageBeanVo);
    }
}
